#include "wuSolidRect.h"


wuSolidRect::wuSolidRect()
{

}

wuSolidRect::~wuSolidRect()
{
//    delete grayImage;
}

void wuSolidRect::setImg(ofImage * _img)
{
			img = _img;
			isDead = false;
};

void wuSolidRect::setDim(float _w, float _h)
{
		w = _w;
		h = _h;
};

void wuSolidRect::update()
{
};

void wuSolidRect::draw(int offX) {

		if(dead) return;

        float ang = getRotation();

		ofSetColor(255, 255, 255);
        ofPushMatrix();
            ofTranslate(getPosition().x+offX, getPosition().y);
            img->setAnchorPercent(0.5, 0.5);
            ofRotateZ(ang);
            ofEnableAlphaBlending();
			img->draw(0, 0, 2*w, 2*h);
            ofDisableAlphaBlending();
        ofPopMatrix();

		if(getPosition().y > 500 || getPosition().x > 700 || getPosition().x < 0)
			isDead = true;
}

void wuSolidRect::touch(int _x,int _y, int forceX, int forceY)
{
		ofPoint pos;

        pos = getPosition();

		if( (_x > pos.x-w) && (_x < pos.x+w) && (_y > pos.y-h) && (_y < pos.y+h) )
		{
			if(forceX>0)
			{
				if( (_x > pos.x-w) && (_x < pos.x))
					this->addImpulseForce(ofPoint(pos.x-w,pos.y), ofPoint(forceX,0));
				if( (_x > pos.x) && (_x < pos.x+w))
					this->addImpulseForce(ofPoint(pos.x+w,pos.y), ofPoint(0-forceX,0));
			}
			if(forceY>0)
			{
				if( (_y > pos.y-h) && (_y < pos.y) )
					this->addImpulseForce(ofPoint(pos.x,pos.y-h), ofPoint(0,0-forceY));
				if( (_y > pos.y) && (_y < pos.y+h) )
					this->addImpulseForce(ofPoint(pos.x,pos.y+h), ofPoint(0,forceY));
			}
		}
}

bool wuSolidRect::drag(int _x,int _y)
{
        bool bDrag=false;
		ofPoint pos;

        pos = getPosition();

		if( (_x > pos.x-w) && (_x < pos.x+w) && (_y > pos.y-h) && (_y < pos.y+h) )
		{
		    setPosition(_x,_y);
            bDrag=true;
		}

	    return bDrag;

}

void wuSolidRect::clear()
{
    destroy();
    img = NULL;
}
